Create a PDF document is the first step to edit a PDF file. Compared with WPF platform, .NET is a more powerful platform that most people may be very familiar with how to get started a PDF file. So in order to satisfy people’s different need, I decide to write some articles about WPF. Then, at the very beginning, it is very necessary to start with how to create a PDF document in WPF with C#, VB.NET.
I always use Spire.PDF for .NET to realize a PDF function in .NET, Now since Spire.PDF for WPF has published long ago. It is time for me to use Spire.PDF for WPF to begin my WPF articles.
You can Freely Download Spire.PDF for WPF
How to create PDF file in WPF with C#, VB.NET
Spire.PDF for WPF is a WPF PDF component which enables your WPF applications to read, write and manipulate PDF documents without using Adobe Acrobat. Please look at the below procedure.
Step1. Create a new project.
I always use Spire.PDF for .NET to realize a PDF function in .NET, Now since Spire.PDF for WPF has published long ago. It is time for me to use Spire.PDF for WPF to begin my WPF articles.
You can Freely Download Spire.PDF for WPF
How to create PDF file in WPF with C#, VB.NET
Spire.PDF for WPF is a WPF PDF component which enables your WPF applications to read, write and manipulate PDF documents without using Adobe Acrobat. Please look at the below procedure.
Step1. Create a new project.
- Create a new project in WPF Application.
- Set the target Framework in Properties of the project to be .NET Framework 4.
- Add a button in MainWindow.
- Add Spire.Pdf.Wpf.dll and System. Drawing as references.
- Add namespaces to the top of the method.
C# Code:
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
VB.NET Code:
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Step2. Create a PDF document.
//create a new pdf document
PdfDocument pdfDoc = new PdfDocument();
//add one blank page
PdfPageBase page = pdfDoc.Pages.Add();
VB.NET Code:
'create a new pdf document
Dim pdfDoc As New PdfDocument()
'add one blank page
Dim page As PdfPageBase = pdfDoc.Pages.Add()
2. Draw text in PDF file.
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
VB.NET Code:
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Step2. Create a PDF document.
- Create a new PDF file and add a blank page in it.
//create a new pdf document
PdfDocument pdfDoc = new PdfDocument();
//add one blank page
PdfPageBase page = pdfDoc.Pages.Add();
VB.NET Code:
'create a new pdf document
Dim pdfDoc As New PdfDocument()
'add one blank page
Dim page As PdfPageBase = pdfDoc.Pages.Add()
2. Draw text in PDF file.
C# Code:
//draw a string on the blank page
PdfFont font = new PdfFont(PdfFontFamily.Courier, 14f);
page.Canvas.DrawString("This sample demonstrates how to write \"Hello, World!\" \n by using Spire.Pdf for WPF!", font, PdfBrushes.Green, new System.Drawing.PointF(0, 20f));
VB.NET Code:
'draw a string on the blank page
Dim font As New PdfFont(PdfFontFamily.Courier, 14F)
page.Canvas.DrawString("This sample demonstrates how to write ""Hello, World!"" " & vbLf & " by using Spire.Pdf for WPF!", font, PdfBrushes.Green, New System.Drawing.PointF(0, 20F))
Step3. Save and launch the file.
C# Code:
//save the pdf document
pdfDoc.SaveToFile(@"sample.pdf");
//launch the pdf document
System.Diagnostics.Process.Start(@"sample.pdf");
VB.NET Code:
'save the pdf document
pdfDoc.SaveToFile("sample.pdf")
'launch the pdf document
System.Diagnostics.Process.Start("sample.pdf")
Preview
//draw a string on the blank page
PdfFont font = new PdfFont(PdfFontFamily.Courier, 14f);
page.Canvas.DrawString("This sample demonstrates how to write \"Hello, World!\" \n by using Spire.Pdf for WPF!", font, PdfBrushes.Green, new System.Drawing.PointF(0, 20f));
VB.NET Code:
'draw a string on the blank page
Dim font As New PdfFont(PdfFontFamily.Courier, 14F)
page.Canvas.DrawString("This sample demonstrates how to write ""Hello, World!"" " & vbLf & " by using Spire.Pdf for WPF!", font, PdfBrushes.Green, New System.Drawing.PointF(0, 20F))
Step3. Save and launch the file.
C# Code:
//save the pdf document
pdfDoc.SaveToFile(@"sample.pdf");
//launch the pdf document
System.Diagnostics.Process.Start(@"sample.pdf");
VB.NET Code:
'save the pdf document
pdfDoc.SaveToFile("sample.pdf")
'launch the pdf document
System.Diagnostics.Process.Start("sample.pdf")
Preview
You also can have a free trial of Spire.Office for WPF since it pakcs Spire.Doc for WPF, Spire.XLS for WPF, Spire.PDF for WPF and Spire.PDFViewer for WPF.